library(dplyr)
library(ggcorrplot)
library(ggpubr)
library(patchwork)
# library("here")
# library("bookdown")
# library("downloadthis")
library(vegan)
library(plyr)
library(e1071)
library(tidyverse)
library(viridis)
library(GGally)
library(ggrepel)
library(ggordiplots)
library(readr)
library(RColorBrewer)
library(oce)
library(plotly)
library(purrr)
library(furrr)
source("gg_ordisurf_viridis.R")
LOAD OBJECTS IF HAVE RUN ALREADY
geodist_pa<-readRDS(file.path(dataPath,"inputs/LoDensNoSB1031p1_geodist_pa.rds"))
geodist_r6<-readRDS(file.path(dataPath,"inputs/LoDensNoSB1031p1_geodist_r6.rds"))
mds_pa<-readRDS(file.path(dataPath,"inputs/LoDensNoSB1031p1_mds_pa.rds"))
mds_r6<-readRDS(file.path(dataPath,"inputs/LoDensNoSB1031p1_mds_r6.rds"))
#mds_r6_1000<-readRDS(file.path(dataPath,"inputs/LoDensNoSB1031p1_mds_r6_1000rep.rds"))
ep<-0.8 #epsilon (to avoid having to find and run just this line from code blocks where the mds objects were made)
env_sort <- read.csv(file.path(dataPath,"inputs/LDensNoSB_env_sort_2022-09-28.csv")) %>% as.data.frame
otu_sort <- read.csv(file.path(dataPath,"inputs/LDensNoSB_otu_sort_2022-09-28.csv")) %>% as.data.frame
table(env_sort$SampID2 == otu_sort$SampID)
#must be same length and equal to unique sample number length - the ordered lists are assumed to be directly relatable on a row by row basis
dim(env_sort)
dim(otu_sort)
env_sort <- env_sort %>% select(-c("BO22_lightbotmean_bdmean",
"BO22_lightbotltmax_bdmean",
"BO22_lightbotltmin_bdmean",
"BO22_lightbotrange_bdmean"))
env_sort_locations<- ggplot(data = env_sort,
aes(x = X.y,
y = Y)) +
theme_classic() +
geom_point(aes(colour = bathy),
size = 2) +
scale_colour_gradient2(low = "red",
mid = "yellow",
high = "green") +
ggtitle("Location of samples in sorted env file")
env_sort_locations
## Removing NAs
otuCompl <- otu_sort[complete.cases(env_sort[, -c(1:23)]), ]
envCompl <- env_sort[complete.cases(env_sort[, -c(1:23)]), ]
## Removing observations with less than 4 OTUs
sel <- rowSums(otuCompl[, -c(1:2)]) >= 4
otuSel <- otuCompl[sel, ]
envSel <- envCompl[sel, ]
## Removing phosphate
#envSel <- envSel %>% select(-phosphate_mean.tif)
dim(otuSel); dim(envSel)
env_sel_locations<- ggplot(data = envSel,
aes(x = X.y,
y = Y)) +
theme_classic() +
geom_point(aes(colour = bathy),
size = 2) +
scale_colour_gradient2(low = "red",
mid = "yellow",
high = "green") +
ggtitle("Location of samples in sorted env file")
env_sel_locations
envCompl_ccrem<-env_sort%>%filter(!SampID%in%envCompl$SampID)
envCompl_ccrem_locations<- ggplot(data = envCompl_ccrem,
aes(x = X.y,
y = Y)) +
theme_classic() +
geom_point(aes(colour = bathy),
size = 2) +
scale_colour_gradient2(low = "red",
mid = "yellow",
high = "green") +
ggtitle("Location of removed complete case samples resulting in envSel file")
envCompl_ccrem_locations
inv.sel <- rowSums(otuCompl[, -c(1:2)]) < 4
env.invSel <- envCompl[inv.sel, ]
env.invSel_locations<- ggplot(data = env.invSel,
aes(x = X.y,
y = Y)) +
theme_classic() +
geom_point(aes(colour = bathy),
size = 2) +
scale_colour_gradient2(low = "red",
mid = "yellow",
high = "green") +
ggtitle("Location of removed samples with <4 OTUs resulting in envSel file")
env.invSel_locations
# otu_red <- otu1[1:500, ]
# otu <- otu_red
#
# env_red <- env[1:500, ]
# env <- env_red
otu<-otuSel[,-c(1)]
env<-envSel[, -c(1,3:23)]
table(is.na(otu))
table(is.na(env))
#str(otuSel)
#str(envSel)
# ## Class 1
# otu1 <- subset(otuSel, envSel$SplitRev == 1)
# env1 <- envSel %>% filter(SplitRev == 1)
#
# ## Class 2
# otu2 <- subset(otuSel, envSel$SplitRev == 2)
# env2 <- envSel %>% filter(SplitRev == 2)
#
# ## Class 4
# otu3 <- subset(otuSel, envSel$SplitRev == 3)
# env3 <- envSel %>% filter(SplitRev == 3)
#
# ## Class 4
# otu4 <- subset(otuSel, envSel$SplitRev == 4)
# env4 <- envSel %>% filter(SplitRev == 4)
#
# ## Class 6
# otu6 <- subset(otuSel, envSel$SplitRev == 6)
# env6 <- envSel %>% filter(SplitRev == 6)
#
# ## Class 7
# otu7 <- subset(otuSel, envSel$SplitRev == 7)
# env7 <- envSel %>% filter(SplitRev == 7)
#
# ## Class 8
# otu8 <- subset(otuSel, envSel$SplitRev == 8)
# env8 <- envSel %>% filter(SplitRev == 8)
# ## Selecting
# otu <- otu2
# env <- env2
#env$coords.x1<-as.numeric(env$coords.x1)
#env$coords.x2<-as.numeric(env$coords.x2)
Make function You might have to drop variables that have been imported as character
otu_pa <- decostand(x = otu[, -c(1)],
method = "pa")
# y = ax^w # power transformation formula
dt <- otu[, -c(1:2)] # species data to transform
x_mn <- min(dt[dt > 0])
x_mx <- max(dt)
rng <- 6 # abundance range
w <- log(rng) / (log(x_mx) - log(x_mn))
a <- x_mn^(-w)
# otu_6 <- a * dt[, -c(1:3)]^w
otu_6 <- a * dt^w
range(otu_6)
Sys.time()
dca_pa <- decorana(veg = otu_pa)
print(dca_pa, head=T)
## Bray-Curtis
dist_pa <- vegdist(x = otu_pa, method = "bray")
## Geodist
ep <- 0.8 # epsilon
geodist_pa <- isomapdist(dist = dist_pa, epsilon = ep)
saveRDS(geodist_pa,
file = (file.path(dataPath,"inputs/LoDensNoSB1031p1_geodist_pa.rds")))
took 6.4mins with jonatan’s paralell solution (usually ~10mins)
#
# Sys.time()
# d <- 2
# mds_pa <- list()
#
# for (i in 1:100) {
# mds_pa[[i]]<-monoMDS(geodist_pa,
# matrix(c(runif(dim(otu_pa)[1]*d)),
# nrow = dim(otu_pa)[1]),
# k = d,
# model = "global",
# maxit = 2000,
# smin = 1e-7,
# sfgrmin = 1e-7)
# }
# Sys.time()
## monoMDS
# d <- 2
# mds_r6 <- list()
#
# Sys.time()
# for (i in 1:200) {
# mds_r6[[i]]<-monoMDS(geodist_r6,
# matrix(c(runif(dim(otu_6)[1]*d)),
# nrow = dim(otu_6)[1]),
# k = d,
# model = "global",
# maxit = 2000,
# smin = 1e-7,
# sfgrmin = 1e-7)
# }
# Sys.time()
#Jonatan's upgrade for speed - parallel processing of mds reptitions
library(furrr) # Package to run non sequential functions in parallel
#library(purrr)
plan(multisession)
d <- 2
i <-200 # number of reps
List_geodist_pa <- lapply(seq_len(i), function(X) geodist_pa) # makes a list with the input data repeated as many times as reps wanted
start_t<-Sys.time()
Xmds_pa<-furrr::future_map(List_geodist_pa,
function(x) monoMDS(x,
matrix(c(runif(dim(otu_6)[1]*d)),
nrow = dim(otu_6)[1]),
k = d,
model = "global",
maxit = 2000,
smin = 1e-7,
sfgrmin = 1e-7),
.progress = TRUE)
Sys.time() - start_t
can take a few mins
saveRDS(Xmds_pa,
file = file.path(dataPath,"inputs/LoDensNoSB1031p1_mds_pa.rds"))
Make function?
# Loading geodist object
# geodist_nfi <- readRDS(file = "../SplitRev2_geodist_pa_full.rds")
# Loading mds results
# mds <- readRDS(file = "../SplitRev2_mds_pa_full.rds")
## Extracting the stress of each nmds iteration
mds_stress_pa<-unlist(lapply(Xmds_pa, function(v){v[[22]]}))
ordered_pa <-order(mds_stress_pa)
## Best, second best, and worst solution
mds_stress_pa[ordered_pa[1]]
[1] 0.2311071
mds_stress_pa[ordered_pa[2]]
[1] 0.2313796
mds_stress_pa[ordered_pa[10]]
[1] 0.2326424
## Scaling of axes to half change units and varimax rotation by postMDS
mds_best_pa<-postMDS(Xmds_pa[[ordered_pa[1]]],
geodist_pa,
pc = TRUE,
halfchange = TRUE,
threshold = ep) # Is this threshold related to the epsilon above?
mds_best_pa
Call:
monoMDS(dist = x, y = matrix(c(runif(dim(otu_6)[1] * d)), nrow = dim(otu_6)[1]), k = d, model = "global", maxit = 2000, smin = 1e-07, sfgrmin = 1e-07)
Non-metric Multidimensional Scaling
942 points, dissimilarity ‘bray shortest isomap’, call ‘isomapdist(dist = dist_pa, epsilon = ep)’
Dimensions: 2
Stress: 0.2311071
Stress type 1, weak ties
Scores scaled to unit root mean square, rotated to principal components
Stopped after 291 iterations: Scale factor of gradient nearly zero (< sfgrmin)
mds_secbest_pa <- postMDS(Xmds_pa[[ordered_pa[2]]],
geodist_pa,
pc = TRUE,
halfchange = TRUE,
threshold = ep)
mds_secbest_pa
Call:
monoMDS(dist = x, y = matrix(c(runif(dim(otu_6)[1] * d)), nrow = dim(otu_6)[1]), k = d, model = "global", maxit = 2000, smin = 1e-07, sfgrmin = 1e-07)
Non-metric Multidimensional Scaling
942 points, dissimilarity ‘bray shortest isomap’, call ‘isomapdist(dist = dist_pa, epsilon = ep)’
Dimensions: 2
Stress: 0.2313796
Stress type 1, weak ties
Scores scaled to unit root mean square, rotated to principal components
Stopped after 232 iterations: Stress nearly unchanged (ratio > sratmax)
## Procrustes comparisons
procr_pa <- procrustes(mds_best_pa,
mds_secbest_pa,
permutations=999)
protest(mds_best_pa,
mds_secbest_pa,
permutations=999)
Call:
protest(X = mds_best_pa, Y = mds_secbest_pa, permutations = 999)
Procrustes Sum of Squares (m12 squared): 0.1863
Correlation in a symmetric Procrustes rotation: 0.9021
Significance: 0.001
Permutation: free
Number of permutations: 999
plot(procr_pa)
png(file=file.path(dataPath,"outputs/LoDensNoSB1031p1_procrustes_pa.png"), width=1000, height=700)
plot(procr_pa)
dev.off()
png
2
# Extracting ordination axis
ax <- 2
axis_pa <- cbind(mds_best_pa$points,
scores(dca_pa,
display = "sites",
origin = TRUE)[, 1:ax])
ggcorr(axis_pa,
method=c("everything","kendall"),
label = TRUE,
label_size = 3,
label_color = "black",
nbreaks = 8,
label_round = 3,
low = "red",
mid = "white",
high = "green")
NA
NA
ggsave(filename = file.path(dataPath,"outputs/LoDensNoSB1031p1_correlationPCAvsNMDS_PA.png"),
device = "png",
dpi=300 )
Saving 7 x 7 in image
# Switching direction of NMDS1
# mds_best$points[, 1] <- -mds_best$points[, 1]
dca_r6 <- decorana(veg = otu_6)
some species were removed because they were missing in the data
print(dca_r6, head=T)
Call:
decorana(veg = otu_6)
Detrended correspondence analysis with 26 segments.
Rescaling of axes with 4 iterations.
DCA1 DCA2 DCA3 DCA4
Eigenvalues 0.4922 0.3074 0.2389 0.1810
Decorana values 0.5815 0.4431 0.3151 0.2671
Axis lengths 4.6535 3.8374 4.7786 5.1129
## Bray-Curtis
dist_r6 <- vegdist(x = otu_6, method = "bray")
## Geodist
ep <- 0.80 # epsilon
geodist_r6 <- isomapdist(dist = dist_r6, epsilon = ep)
saveRDS(geodist_r6,
file = (file.path(dataPath,"inputs/LoDensNoSB1031p1_geodist_r6.rds")))
Jonatan’s parallel solution took 5.4mins, before it took 10mins
## monoMDS
# d <- 2
# mds_r6 <- list()
#
# Sys.time()
# for (i in 1:200) {
# mds_r6[[i]]<-monoMDS(geodist_r6,
# matrix(c(runif(dim(otu_6)[1]*d)),
# nrow = dim(otu_6)[1]),
# k = d,
# model = "global",
# maxit = 2000,
# smin = 1e-7,
# sfgrmin = 1e-7)
# }
# Sys.time()
#Jonatan's upgrade for speed - parallel processing of mds reptitions
#library(furrr) # Package to run non sequential functions in parallel
#library(purrr)
plan(multisession)
d <- 2
i <-200 # number of reps
List_geodist_r6 <- lapply(seq_len(i), function(X) geodist_r6) # makes a list with the input data repeated as many times as reps wanted
start_t<-Sys.time()
Xmds_r6<-furrr::future_map(List_geodist_r6,
function(x) monoMDS(x,
matrix(c(runif(dim(otu_6)[1]*d)),
nrow = dim(otu_6)[1]),
k = d,
model = "global",
maxit = 2000,
smin = 1e-7,
sfgrmin = 1e-7),
.progress = TRUE)
Sys.time() - start_t
saveRDS(Xmds_r6,
file = file.path(dataPath,"inputs/LoDensNoSB1031p1_mds_r6.rds"))
# Loading geodist object
# geodist_nfi <- readRDS(file = "../SplitRev2_geodist_nfi.rds")
# Loading mds results
# mds <- readRDS(file = "../SplitRev2_mds.rds")
## Extracting the stress of each nmds iteration
mds_stress_r6<-unlist(lapply(Xmds_r6, function(v){v[[22]]}))
ordered_r6 <-order(mds_stress_r6)
## Best, second best, and worst solution
mds_stress_r6[ordered_r6[1]]
[1] 0.2246436
mds_stress_r6[ordered_r6[2]]
[1] 0.2255269
mds_stress_r6[ordered_r6[100]]
[1] 0.229723
## Scaling of axes to half change units and varimax rotation by postMDS
mds_best_r6<-postMDS(Xmds_r6[[ordered_r6[1]]],
geodist_r6,
pc = TRUE,
halfchange = TRUE,
threshold = ep) # Is this threshold related to the epsilon above?
mds_best_r6
Call:
monoMDS(dist = x, y = matrix(c(runif(dim(otu_6)[1] * d)), nrow = dim(otu_6)[1]), k = d, model = "global", maxit = 2000, smin = 1e-07, sfgrmin = 1e-07)
Non-metric Multidimensional Scaling
942 points, dissimilarity ‘bray shortest isomap’, call ‘isomapdist(dist = dist_r6, epsilon = ep)’
Dimensions: 2
Stress: 0.2246436
Stress type 1, weak ties
Scores scaled to unit root mean square, rotated to principal components
Stopped after 168 iterations: Scale factor of gradient nearly zero (< sfgrmin)
mds_secbest_r6<-postMDS(Xmds_r6[[ordered_r6[2]]],
geodist_r6,
pc = TRUE,
halfchange = TRUE,
threshold = ep)
mds_secbest_r6
Call:
monoMDS(dist = x, y = matrix(c(runif(dim(otu_6)[1] * d)), nrow = dim(otu_6)[1]), k = d, model = "global", maxit = 2000, smin = 1e-07, sfgrmin = 1e-07)
Non-metric Multidimensional Scaling
942 points, dissimilarity ‘bray shortest isomap’, call ‘isomapdist(dist = dist_r6, epsilon = ep)’
Dimensions: 2
Stress: 0.2255269
Stress type 1, weak ties
Scores scaled to unit root mean square, rotated to principal components
Stopped after 139 iterations: Scale factor of gradient nearly zero (< sfgrmin)
## Procrustes comparisons
procr_r6 <- procrustes(mds_best_r6,
mds_secbest_r6,
permutations=999)
protest(mds_best_r6,
mds_secbest_r6,
permutations=999)
Call:
protest(X = mds_best_r6, Y = mds_secbest_r6, permutations = 999)
Procrustes Sum of Squares (m12 squared): 0.1932
Correlation in a symmetric Procrustes rotation: 0.8982
Significance: 0.001
Permutation: free
Number of permutations: 999
plot(procr_r6)
png(file.path(dataPath,"outputs/LoDensNoSB1031p1_procrustes_r6.png"), width=1000, height=700,) #added 1000
plot(procr_r6)
dev.off()
png
2
#### 1000 reps - don’t run if loading saved object Currently commented out as it gained nothing but took extra time. Can be removed in due course.
retain the 200 rep version
# Extracting ordination axis
ax <- 2
axis_r6 <- cbind(mds_best_r6$points,
scores(dca_r6,
display = "sites",
origin = TRUE)[, 1:ax])
ggcorr(axis_r6,
method=c("everything","kendall"),
label = TRUE,
label_size = 3,
label_color = "black",
nbreaks = 8,
label_round = 3,
low = "red",
mid = "white",
high = "green")
ggsave(filename = file.path(dataPath,"outputs/LoDensNoSB1031p1_correlationPCAvsNMDS_r6.png"),
device = "png",
dpi=300 )
Saving 7 x 7 in image
# Switching direction of NMDS1
# mds_best$points[, 1] <- -mds_best$points[, 1]
## Adding scores to data frame
otu_6$gnmds1 <- mds_best_r6$points[, 1]
otu_6$gnmds2 <- mds_best_r6$points[, 2]
otu_6$dca1 <- scores(dca_r6, display = "sites", origin = TRUE)[, 1]
otu_6$dca2 <- scores(dca_r6, display = "sites", origin = TRUE)[, 2]
p_gnmds_r6 <- ggplot(data = otu_6,
aes(x = gnmds1,
y = gnmds2)) +
theme_classic() +
coord_fixed() +
ggtitle("GNMDS",
subtitle = "First run") +
geom_point(colour = "red") +
geom_vline(xintercept = 0,
linetype = 2,
colour = "lightgray") +
geom_hline(yintercept = 0,
linetype = 2,
colour = "lightgray")
p_dca_r6 <- ggplot(data = otu_6,
aes(x = dca1,
y = dca2)) +
theme_classic() +
coord_fixed() +
ggtitle("DCA",
subtitle = "First run") +
geom_point(colour = "red") +
geom_vline(xintercept = 0,
linetype = 2,
colour = "lightgray") +
geom_hline(yintercept = 0,
linetype = 2,
colour = "lightgray")
p_gnmds_r6 + p_dca_r6
NA
NA
ggsave(file.path(dataPath,"outputs/LoDensNoSB1031p1_gnmds_dca_r6.png"),
device = "png",
dpi=300)
Saving 7 x 7 in image
ord <- mds_best_r6
## Axis scores if selected ord is GNMDS
axis <- ord$points %>% as.data.frame
## Axis scores if selected ord is DCA
# axis <- scores(ord,
# display = "sites",
# origin = TRUE)[, 1:ax])
decided to make MLD-bathy vars
env<-env %>%
mutate ("MLDmean_bathy"=MLDmean_Robinson-(bathy*-1),
"MLDmin_bathy"=MLDmin_Robinson-(bathy*-1),
"MLDmax_bathy"=MLDmax_Robinson-(bathy*-1))
env$MLDmean_bathy<-cut(env$MLDmean_bathy,
breaks=c(-2560, -20,20,130),#checked range of values first (min -2554, max 123)
labels=c('belowMLD','onPycno','inMixLayer'))
env$MLDmin_bathy<-cut(env$MLDmin_bathy,
breaks=c(-2560, -20,20,130),#checked range of values first (min -2554, max 123)
labels=c('belowMLD','onPycno','inMixLayer'))
env$MLDmax_bathy<-cut(env$MLDmax_bathy,
breaks=c(-2560, -20,20,130),#checked range of values first (min -2554, max 123)
labels=c('belowMLD','onPycno','inMixLayer'))
env$swDensRob_avs<-swRho(salinity=env$Smean_Robinson,
temperature=env$Tmean_Robinson,
pressure=(env$bathy*-1),
eos="unesco")
env_cont<-env%>% select(-c(landscape,sedclass,gmorph, MLDmean_bathy, MLDmax_bathy, MLDmin_bathy, #categorical
optional, #not a var
MLDmax_Robinson, MLDmean_Robinson, MLDmin_Robinson, #replaced by new vars
MLDsd_Robinson #not meaningful
))
env_cont<-env_cont%>% mutate_if(is.integer,as.numeric)
env_corr <- env_cont %>% select(-c(SampID))
# env_corr$coords.x1<-as.numeric(env_corr$coords.x1)
# env_corr$coords.x2<-as.numeric(env_corr$coords.x2)
env_corr[(!is.numeric(env_corr)),]
# Vector to hold correlations
cor_ax1 <- NULL
cor_ax2 <- NULL
pv_ax1 <- NULL
pv_ax2 <- NULL
# NMDS1
for( i in seq(length(env_corr))) {
ct.i <- cor.test(axis$MDS1,
env_corr[, i],
method = "kendall")
cor_ax1[i] <- ct.i$estimate
pv_ax1[i] <- ct.i$p.value
}
# NMDS2
for( i in seq(length(env_corr))) {
ct.i <- cor.test(axis$MDS2,
env_corr[, i],
method = "kendall")
cor_ax2[i] <- ct.i$estimate
pv_ax2[i] <- ct.i$p.value
}
cor_tab <- data.frame(env = names(env_corr),
ord_ax1 = cor_ax1,
pval_ax1 = pv_ax1,
ord_ax2 = cor_ax2,
pval_ax2 = pv_ax2)
cor_tab
write.csv(x = cor_tab,
file = file.path(dataPath,"inputs/LoDensNoSB1031p1_cor-table_r6_200rep_MLD-bathy.csv"),
row.names = FALSE)
cor_a1_sort<-cor_tab%>%
mutate(abs_ord_ax1=abs(ord_ax1),
abs_ord_ax2=abs(ord_ax2)) %>%
arrange(desc(abs_ord_ax1))
cor_a2_sort<-cor_tab%>%
mutate(abs_ord_ax1=abs(ord_ax1),
abs_ord_ax2=abs(ord_ax2)) %>%
arrange(desc(abs_ord_ax2))
dotchart(cor_a1_sort$abs_ord_ax1, main="Absolute (+/-) correlations between envVars and gnmds axis 1")
cor_cut<-0.4 #decide
cor_sel<-subset(cor_a1_sort,abs_ord_ax1>cor_cut)
cor_sel
as.data.frame(cor_sel$env)
env_os <- env[, cor_sel$env]
env_os
str(env_os)
'data.frame': 942 obs. of 19 variables:
$ BO22_icecoverrange_ss : num 0 0 0 0 0 0 0 0 0 0 ...
$ BO22_icethickrange_ss : num 0 0 0 0 0 0 0 0 0 0 ...
$ BO22_icethickltmax_ss : num 0 0 0 0 0 0 0 0 0 0 ...
$ BO22_icecoverltmax_ss : num 0 0 0 0 0 0 0 0 0 0 ...
$ BO22_icethickmean_ss : num 0 0 0 0 0 0 0 0 0 0 ...
$ Tmean_Robinson : num 7.87 7.85 6.92 4.27 7.36 ...
$ BO22_icecovermean_ss : num 0 0 0 0 0 0 0 0 0 0 ...
$ Tmin_Robinson : num 7.72 7.74 6.6 3.92 7.23 ...
$ Tmax_Robinson : num 8.03 8.03 7.08 4.62 7.44 ...
$ temp_mean : num 6.22 5.93 4.33 1.78 5.34 ...
$ BO22_dissoxltmin_bdmean : num 262 263 263 264 263 ...
$ temp_max : num 8.08 8.15 7.53 6.76 7.95 ...
$ BO22_dissoxmean_bdmean : num 285 286 286 286 286 ...
$ BO22_dissoxrange_bdmean : num 285 286 286 286 286 ...
$ BO22_carbonphytoltmin_ss: num 0.363 0.356 0.345 0.333 0.362 ...
$ Y : num 7200734 7206934 7209334 7203134 7191534 ...
$ coords.x2 : num 7200725 7207024 7209322 7203046 7191557 ...
$ BO22_chloltmin_ss : num 0.123 0.129 0.128 0.124 0.132 ...
$ BO22_ppltmin_ss : num 0.000174 0.000171 0.00016 0.000153 0.000182 ...
ordsrfs <- list(length = ncol(env_os))
for (i in seq(ncol(env_os))) {
os.i <- gg_ordisurf(ord = ord,
env.var = env_os[, i],
pt.size = 1,
# binwidth = 0.05,
var.label = names(env_os)[i],
gen.text.size = 10,
title.text.size = 15,
leg.text.size = 10)
ordsrfs[[i]] <- os.i$plot
}
ordsrfs_plt <- ggarrange(plotlist = ordsrfs,
nrow = 5,
ncol = 4)
ordsrfs_plt
ggexport(ordsrfs_plt,
filename = file.path(dataPath,"outputs/LoDensNoSB1031p1_ordisurfs_top_corr.png"),
width = 2000,
height = 2500)
env_os_m <- env[,c("Tmean_Robinson", #top corr
There were 15 warnings (use warnings() to see them)
"salt_max", #top corr
"Smax_Robinson", #comparison to top corr
"swDensRob_avs", #top corr
"BO22_icecoverltmax_ss",#top corr ax2
"BO22_icecovermean_ss",#top corr
"BO22_dissoxmean_bdmean",#top corr
#"BO22_carbonphytoltmin_bdmean",#top corr - no clear gradient in ordisurf
"BO22_ppltmin_ss", #top corr
"X.y", #comparison to Y
"Y", #top corr
"spd_std", #top corr ax2 (blended model)
"CSpdsd_Robinson", #comparison to top corr ax2 (blended model)
"mud", #highest sed var ax1 + corr
"gravel",#highest sed var ax1 - corr
"BO22_silicateltmax_bdmean", #just under top corr ax1
"bathy" #intuitive for comparisons
)]
env_os_m
str(env_os_m)
'data.frame': 942 obs. of 16 variables:
$ Tmean_Robinson : num 7.87 7.85 6.92 4.27 7.36 ...
$ salt_max : num 35.2 35.2 35.2 35.1 35.2 ...
$ Smax_Robinson : num 35.4 35.4 35.4 35.2 35.4 ...
$ swDensRob_avs : num 1029 1029 1030 1031 1030 ...
$ BO22_icecoverltmax_ss : num 0 0 0 0 0 0 0 0 0 0 ...
$ BO22_icecovermean_ss : num 0 0 0 0 0 0 0 0 0 0 ...
$ BO22_dissoxmean_bdmean : num 285 286 286 286 286 ...
$ BO22_ppltmin_ss : num 0.000174 0.000171 0.00016 0.000153 0.000182 ...
$ X.y : num 76461 67461 57261 50461 61261 ...
$ Y : num 7200734 7206934 7209334 7203134 7191534 ...
$ spd_std : num 0.074 0.096 0.114 0.098 0.114 ...
$ CSpdsd_Robinson : num 0.01692 0.00818 0.0035 0.00648 0.00254 ...
$ mud : num 26 18 8 8 1 26 18 1 7 1 ...
$ gravel : num 36 55 21 21 33 36 55 35 65 35 ...
$ BO22_silicateltmax_bdmean: num 6.29 6.27 7.41 9.03 6.91 ...
$ bathy : num -325 -334 -481 -604 -435 ...
ordsrfs_m <- list(length = ncol(env_os_m))
for (i in seq(ncol(env_os_m))) {
os.i_m <- gg_ordisurf(ord = ord,
env.var = env_os_m[, i],
pt.size = 1,
# binwidth = 0.05,
var.label = names(env_os_m)[i],
gen.text.size = 10,
title.text.size = 15,
leg.text.size = 10)
ordsrfs_m[[i]] <- os.i_m$plot
}
ordsrfs_plt_m <- ggarrange(plotlist = ordsrfs_m,
nrow = 4,
ncol = 4)
ordsrfs_plt_m
ggexport(ordsrfs_plt_m,
filename = file.path(dataPath,"outputs/LoDensNoSB1031p1_ordisurfs_man_sel_domean.png"),
width = 2000,
height = 2000)
## Select if any var should be excluded from envfit (makes less busy to read)
env_os_m_envfit<-env_os_m [,c("Tmean_Robinson", #top corr
"salt_max", #top corr
"Smax_Robinson", #comparison to top corr
"swDensRob_avs", #top corr
"BO22_icecoverltmax_ss",#top corr ax2
#"BO22_icecovermean_ss",#top corr
"BO22_dissoxmean_bdmean",#top corr
#"BO22_dissoxltmin_bdmean",#top corr
#"BO22_carbonphytoltmin_bdmean",#top corr - no clear gradient in ordisurf
"BO22_ppltmin_ss", #top corr
"X.y", #comparison to Y
"Y", #top corr
"spd_std", #top corr ax2 (blended model)
# "CSpdsd_Robinson", #comparison to top corr ax2 (blended model)
"mud", #highest sed var ax1 + corr
"gravel",#highest sed var ax1 - corr
"BO22_silicateltmax_bdmean", #just under top corr ax1
"bathy" #intuitive for comparisons
)]
colnames(env_os_m_envfit)<-c("T", #top corr
"sMx", #top corr
"SmaxR", #comparison to top corr
"swDensR", #top corr
"icecovmax",#top corr ax2
#"icecovav",#top corr
"dissoxav",#top corr
#"dissoxmin",#top corr
#"BO22_carbonphytoltmin_bdmean",#top corr - no clear gradient in ordisurf
"ppltmin", #top corr
"X", #comparison to Y
"Y", #top corr
"spd_std", #top corr ax2 (blended model)
# "CSsd", #comparison to top corr ax2 (blended model)
"mud", #highest sed var ax1 + corr
"gravel",#highest sed var ax1 - corr
"SiLtmax", #just under top corr ax1
"bathy" #intuitive for comparisons
)
## Envfot plot
gg_envfit(ord = ord,
env = env_os_m_envfit,
pt.size = 1)
## Envfit analysis
ef <- envfit(ord = ord,
env = env_os_m_envfit,
# na.rm = TRUE
)
efDF <- as.data.frame(scores(ef,
display = "vectors"))
ggsave(filename = file.path(dataPath,"outputs/LoDensNoSB1031p1_EnvFit_man_sel_cln_domean.png"),
device = "png",
dpi=300 )
Saving 7 x 7 in image
env_vis<-env
env_vis$gnmds1 <- otu_6$gnmds1
env_vis$gnmds2 <- otu_6$gnmds2
env_vis$dca1 <- otu_6$dca1
env_vis$dca2 <- otu_6$dca2
dca_int <- ggplot(data = env_vis,
aes(x = dca1,
y = dca2)) +
theme_classic() +
coord_fixed() +
ggtitle("Interactive DCA sample ID plot",
subtitle = "First run") +
geom_point(aes(colour = factor(SampID))) +
geom_vline(xintercept = 0,
linetype = 2,
colour = "lightgray") +
geom_hline(yintercept = 0,
linetype = 2,
colour = "lightgray")
ggplotly(dca_int)
p_mld <- ggplot(data = env_vis,
There were 11 warnings (use warnings() to see them)
aes(x = gnmds1,
y = gnmds2)) +
theme_classic() +
coord_fixed() +
ggtitle("GNMDS coloured by proximity to mixed layer depth",
subtitle = "First run") +
geom_point(aes(colour = MLDmean_bathy)) +
geom_vline(xintercept = 0,
linetype = 2,
colour = "lightgray") +
geom_hline(yintercept = 0,
linetype = 2,
colour = "lightgray")
p_mld
env_vis<- env_vis %>%
mutate(
sedclassName = case_when(
sedclass == "1" ~ "SedCoverR",
sedclass == "5" ~ "Rock",
sedclass == "20" ~ "Mud",
sedclass == "21" ~ "MwBlock",
sedclass == "40" ~ "sMud",
sedclass == "80" ~ "mSand",
sedclass == "100" ~ "Sand",
sedclass == "110" ~ "gMud",
sedclass == "115" ~ "gsMud",
sedclass == "120" ~ "gmSand",
sedclass == "130" ~ "gSand",
sedclass == "150" ~ "MSG",
sedclass == "160" ~ "sGravel",
sedclass == "170" ~ "Gravel",
sedclass == "175" ~ "GravBlock",
sedclass == "185" ~ "SGBmix",
sedclass == "205" ~ "S/MwB",
sedclass == "206" ~ "S/MwG/B",
sedclass == "215" ~ "SGBalt",
sedclass == "300" ~ "HardSed",
sedclass == "500" ~ "Biogenic"
)
)
c25 <- c(
"dodgerblue2", "#E31A1C", # red
"green4",
"#6A3D9A", # purple
"#FF7F00", # orange
"black", "gold1",
"skyblue2", "#FB9A99", # lt pink
"palegreen2",
"#CAB2D6", # lt purple
"#FDBF6F", # lt orange
"gray70", "khaki2",
"maroon", "orchid1", "deeppink1", "blue1", "steelblue4",
"darkturquoise", "green1", "yellow4", "yellow3",
"darkorange4", "brown"
)
p_sed <- ggplot(data = env_vis,
aes(x = gnmds1,
y = gnmds2)) +
theme_classic() +
coord_fixed() +
ggtitle("GNMDS coloured by sediment class",
subtitle = "First run") +
geom_point(aes(colour = factor(sedclassName))) +
scale_colour_manual(values=c25)+
geom_vline(xintercept = 0,
linetype = 2,
colour = "lightgray") +
geom_hline(yintercept = 0,
linetype = 2,
colour = "lightgray")+
guides(colour=guide_legend(ncol=2))
p_sed
env_vis<- env_vis %>%
mutate(
landscapeName = case_when(
landscape == "1" ~ "Strandflat",
landscape == "21" ~ "ContSlope",
landscape == "22" ~ "Canyon",
landscape == "31" ~ "Valley",
landscape == "32" ~ "Fjord",
landscape == "41" ~ "DeepPlain",
landscape == "42" ~ "SlopePlain",
landscape == "43" ~ "ShelfPlain",
landscape == "431" ~ "shallowValley"
)
)
p_land <- ggplot(data = env_vis,
aes(x = gnmds1,
y = gnmds2)) +
theme_classic() +
coord_fixed() +
ggtitle("GNMDS coloured by landscape class",
subtitle = "First run") +
geom_point(aes(colour = factor(landscapeName))) +
geom_vline(xintercept = 0,
linetype = 2,
colour = "lightgray") +
geom_hline(yintercept = 0,
linetype = 2,
colour = "lightgray")+
guides(colour=guide_legend(ncol=2))
p_land
p_gmo <- ggplot(data = env_vis,
aes(x = gnmds1,
y = gnmds2)) +
theme_classic() +
coord_fixed() +
ggtitle("GNMDS coloured by landscape class",
subtitle = "First run") +
geom_point(aes(colour = factor(gmorph))) +
geom_vline(xintercept = 0,
linetype = 2,
colour = "lightgray") +
geom_hline(yintercept = 0,
linetype = 2,
colour = "lightgray")+
guides(colour=guide_legend(ncol=2))
p_gmo
cat_var_plots<-p_mld+p_sed+p_land+p_gmo
ggexport(cat_var_plots,
filename = file.path(dataPath,"outputs/LoDensNoSB1031p1_gnmds_catvar.png"),
width = 1000,
height = 800)
p_gmo <- ggplot(data = env_vis,
aes(x = gnmds1,
y = gnmds2)) +
theme_classic() +
coord_fixed() +
ggtitle("GNMDS coloured by sample",
subtitle = "First run") +
geom_point(aes(colour = factor(SampID))) +
geom_vline(xintercept = 0,
linetype = 2,
colour = "lightgray") +
geom_hline(yintercept = 0,
linetype = 2,
colour = "lightgray")+
guides(colour="none", size="none")
ggplotly(p_gmo)